package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.SingleItemBean;
import com.wnhz.work.yz.molder.EntiretyShoppingMolder;
import com.wnhz.work.yz.utils.SystemUtils;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddCartActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "AddCartActivity";
    private EditText shopping_number;
    private ImageView reduce;
    private List<SingleItemBean> mList;
    private EntiretyShoppingMolder molder;

    private EditText room_ed;
    private EditText remark_ed;
    private String money;

    private TextView tv_all_price;
    private RelativeLayout btn_main_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cart);
        initTitle();
        molder = (EntiretyShoppingMolder) getIntent().getSerializableExtra("molder");
        money = getIntent().getStringExtra("money");

        Log.e("====参数模型====", "" + molder);

        btn_main_right = (RelativeLayout) findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.GONE);
        room_ed = (EditText) findViewById(R.id.room_ed);
        remark_ed = (EditText) findViewById(R.id.remark_ed);
        tv_all_price = (TextView) findViewById(R.id.tv_all_price);
        reduce = (ImageView) findViewById(R.id.reduce);

        reduce.setOnClickListener(this);
        findViewById(R.id.ll_add).setOnClickListener(this);
        findViewById(R.id.tv_add_card).setOnClickListener(this);
        DecimalFormat df = new DecimalFormat("######0.00");
        tv_all_price.setText("¥ " + df.format(Double.parseDouble(money)));
        shopping_number = (EditText) findViewById(R.id.shopping_number);
        shopping_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int numS = 0;
                try {
                    String num = shopping_number.getText().toString();
                    numS = Integer.parseInt(num);
                } catch (Exception e) {
                    numS = 0;
                    num=0;
                }
                DecimalFormat df = new DecimalFormat("######0.00");
                num=numS;
                tv_all_price.setText("¥ " + df.format(Double.parseDouble(money) * numS));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.add_cart_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);

    }

    private int num;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.reduce://减少
                if (num > 1) {
                    num--;
                }
                if (num > 1) {
                    reduce.setImageResource(R.mipmap.ic_decrease_li);
                } else {
                    reduce.setImageResource(R.mipmap.ic_decrease_nor);
                }
                shopping_number.setText(num + "");
                break;

            case R.id.ll_add://增加
                num++;
                shopping_number.setText(num + "");
                break;

            //加入购物车
            case R.id.tv_add_card:
                String room_number = room_ed.getText().toString();
                String intro = remark_ed.getText().toString();
                String nums = shopping_number.getText().toString();
                molder.setIntro(intro);
                molder.setRoom_number(room_number + "");
                molder.setNums(nums);
                showDialog();
                final String js = JSON.toJSONString(molder);
                Map<String, Object> map = new HashMap<>();
                map.put("token", MyApplication.getInstance().getUserBean().getToken());
                map.put("json_str", js);

                map.put("equipment", "android");
                map.put("version", "" + SystemUtils.getVersionName(AddCartActivity.this));


                XUtil.Post(Url.PACKAGE_PACKAGEADDCART, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        Log.e(TAG, "onSuccess:json_str= " + result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
                            String info = jsonObject.optString("info");
                            if ("1".equals(res)) {
                                MyToast(info);
                                Intent intent = new Intent(AddCartActivity.this, BathroomDetailActivity.class);
                                startActivity(intent);
                            } else {
                                MyToast(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished() {
                        closeDialog();
                        super.onFinished();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        closeDialog();
                    }
                });
                // } else {
                //     MyToast("请将数据填写完整!");
                // }
                break;
        }
    }
}
